package y2015.date0917pm.mysql连接2;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


public class DBOpenHelper {
    @Test
    public void main() throws Exception {
        Connection conn = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("注册jar包的驱动类->完成");
            String host = "127.0.0.1";
            String port = "3306";
            String database = "tarena_chat";
            String url = "jdbc:mysql://" + host + ":" + port + "/" + database;
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);

            System.out.println("连接数据库成功");

            //Statement用于执行静态 SQL 语句并返回它所生成结果的对象。
            Statement stmt = null;
            try {
                stmt = conn.createStatement();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("获取Statement->完成");

            String sql;
            //			 增加数据记录
            sql =
                    "insert into user (username, password) values (teacher, 999999)";

//			(1) 对于 SQL 数据操作语言 (DML) 语句，返回行计数 
//			(2) 对于什么都不返回的 SQL 语句，返回 0 

            int affectedRows = stmt.executeUpdate(sql);
            System.out.println("增加记录完成，受影响的行数：" + affectedRows);

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

